import datetime
import time

from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.conf import settings
from django.views.decorators.cache import cache_page
from stocksys import models as stocksys_models
from django.contrib.auth import models as auth_models
import requests as sys_requests
from stocksys.sys_tasks.logic_function import get_realtime_stock_content
from stocksys.views.data_logic import resources2infojson, getResourceBySpider, get_day_exchange_time,\
    get_month_exchange_time, init_message, get_stock_json_info, get_one_day_call_auction, get_one_day_mtss, \
    get_stock_company_info
from stocksys.views.user_views import check_login


@check_login
@cache_page(60)
def stock_list(request):
    user = auth_models.User.objects.get(id=request.session.get('id'))
    sysconf = stocksys_models.SysConf.objects.get(user=user)
    context = init_message(request)
    context["title"] = "量化分析系统-股票列表"
    _stocks = stocksys_models.Stock.objects.all().order_by("id")
    exchanges = []
    for exchange in _stocks:
        stock_code = "%s%s" % (exchange.exchange_address.stock_sina_name, exchange.code)
        info = get_realtime_stock_content(stock_code)
        exchange_chg = 0
        if info['current'] != 0:
            exchange_chg = (info['current'] - info['close']) / info['close']
        if exchange_chg < 0:
            color = 'success'
        elif exchange_chg == 0:
            color = 'warning'
        else:
            color = 'danger'
        exchanges.append({
            'name': exchange.name,
            'abbreviated_name': exchange.abbreviated_name,
            'exchange_address': exchange.exchange_address,
            'code': exchange.code,
            'type': exchange.type,

            'open': info['open'],
            'close': info['close'],
            'current': info['current'],
            'chg': exchange_chg * 100,
            'chg_price': exchange_chg,
            'color': color,
            'volume': info['v_num'] / 10000,
            'money': info['money'] / 10000,
            "systime": datetime.datetime.now().strftime('%Y-%m-%d'),
        })
    context["exchanges"] = exchanges
    context["now_year"] = int(time.strftime('%Y', time.localtime(time.time())))
    context["quant_start_date"] = sysconf.predict_start_time.strftime("%Y-%m-%d")
    context["quant_end_date"] = sysconf.predict_end_time.strftime("%Y-%m-%d")
    context["predict_type"] = 'stock'
    context["data_method"] = 'sina'
    if settings.USE_JQDATA_SDK:
        context["data_method"] = 'jqdata'
    return render(request, "stocksys/table_list/exchange_table.html", context)


@check_login
@cache_page(60)
def stock_info(request, code, addr, year):
    exchange_address = stocksys_models.ExchangeAddress.objects.get(id=addr)
    stock = stocksys_models.Stock.objects.get(code=code, exchange_address=exchange_address)

    if request.method == "GET":
        context = init_message(request)
        context["call_auction"] = None
        context["title"] = "量化分析系统-股票信息"
        context["data_method"] = 'sina'
        context["info"] = get_stock_company_info(stock)
        if settings.USE_JQDATA_SDK:
            context["data_method"] = 'jqdata'
        if settings.USE_JQDATA_SDK:

            # 获取集合竞价数据 call_auction

            context["call_auction"] = get_one_day_call_auction(stock)

            # 融资融券信息 mtss

            context["mtss"] = get_one_day_mtss(stock)

        else:
            context["call_auction"] = None
        context["stock_address"] = stock.exchange_address.stock_address_name
        context["stock"] = stock
        return render(request, "stocksys/info/stock_info.html", context)
    elif request.method == 'POST':
        info = get_stock_json_info(stock)
        return JsonResponse(info)


# 股票分时数据
@check_login
def stock_time_info(request):
    if request.method == "POST":
        info = {"statue": 200, "data": [["0930", 33.02, 33.02, 8200],
                                        ["0931", 33.08, 33.05, 54200],
                                        ["0932", 33.15, 33.083, 25700],
                                        ["0933", 33.17, 33.105, 20300],
                                        ["0934", 33.2, 33.124, 20500],
                                        ["0935", 33.31, 33.155, 47600],
                                        ["0936", 33.42, 33.193, 42200],
                                        ["0937", 33.43, 33.223, 28500],
                                        ["0938", 33.36, 33.238, 23500],
                                        ["0939", 33.39, 33.253, 27100],
                                        ["0940", 33.4, 33.266, 18300],
                                        ["0941", 33.37, 33.275, 13300],
                                        ["0942", 33.32, 33.278, 18700],
                                        ["0943", 33.27, 33.278, 16000],
                                        ["0944", 33.23, 33.275, 13000],
                                        ["0945", 33.2, 33.27, 20100],
                                        ["0946", 33.22, 33.267, 18900],
                                        ["0947", 33.24, 33.266, 6000],
                                        ["0948", 33.18, 33.261, 26100],
                                        ["0949", 33.13, 33.255, 11300],
                                        ["0950", 33.16, 33.25, 9200],
                                        ["0951", 33.12, 33.244, 10800],
                                        ["0952", 33.02, 33.234, 15700],
                                        ["0953", 33.05, 33.227, 11500],
                                        ["0954", 33.11, 33.222, 2200],
                                        ["0955", 33.09, 33.217, 12800],
                                        ["0956", 33.08, 33.212, 19200],
                                        ["0957", 33.05, 33.206, 6400],
                                        ["0958", 33.08, 33.202, 12800],
                                        ["0959", 33.08, 33.198, 4500],
                                        ["1000", 33.14, 33.196, 3100],
                                        ["1001", 33.2, 33.196, 11950],
                                        ["1002", 33.18, 33.195, 16221],
                                        ["1003", 33.18, 33.195, 12400],
                                        ["1004", 33.16, 33.194, 5100],
                                        ["1005", 33.16, 33.193, 4400],
                                        ["1006", 33.1, 33.191, 23700],
                                        ["1007", 33.1, 33.188, 9400],
                                        ["1008", 33.05, 33.185, 11200],
                                        ["1009", 33.08, 33.182, 8900],
                                        ["1010", 33.04, 33.179, 9000],
                                        ["1011", 33.01, 33.175, 14200],
                                        ["1012", 32.97, 33.17, 7100],
                                        ["1013", 33.01, 33.166, 5700],
                                        ["1014", 33.01, 33.163, 8900],
                                        ["1015", 33.0, 33.159, 3100],
                                        ["1016", 33.0, 33.156, 4000],
                                        ["1017", 32.96, 33.152, 11900],
                                        ["1018", 32.89, 33.146, 11400],
                                        ["1019", 32.91, 33.142, 12900],
                                        ["1020", 32.9, 33.137, 8400],
                                        ["1021", 32.88, 33.132, 7300],
                                        ["1022", 32.9, 33.128, 15300],
                                        ["1023", 32.93, 33.124, 20900],
                                        ["1024", 32.94, 33.121, 11000],
                                        ["1025", 32.9, 33.117, 2800],
                                        ["1026", 32.94, 33.114, 2600],
                                        ["1027", 32.92, 33.11, 1200],
                                        ["1028", 32.9, 33.107, 6700],
                                        ["1029", 32.89, 33.103, 6400],
                                        ["1030", 32.89, 33.1, 3300],
                                        ["1031", 32.92, 33.097, 7000],
                                        ["1032", 32.89, 33.093, 4700],
                                        ["1033", 32.9, 33.09, 7100],
                                        ["1034", 32.89, 33.087, 4300],
                                        ["1035", 32.89, 33.084, 3700],
                                        ["1036", 32.9, 33.081, 2200],
                                        ["1037", 32.9, 33.079, 6700],
                                        ["1038", 32.94, 33.077, 1500],
                                        ["1039", 32.94, 33.075, 4400],
                                        ["1040", 32.91, 33.073, 4000],
                                        ["1041", 32.91, 33.07, 9100],
                                        ["1042", 32.91, 33.068, 1600],
                                        ["1043", 32.91, 33.066, 8000],
                                        ["1044", 32.91, 33.064, 8600],
                                        ["1045", 32.94, 33.062, 2300],
                                        ["1046", 32.94, 33.061, 900],
                                        ["1047", 32.93, 33.059, 7900],
                                        ["1048", 32.93, 33.057, 7500],
                                        ["1049", 32.92, 33.056, 5700],
                                        ["1050", 32.94, 33.054, 2500],
                                        ["1051", 33.0, 33.054, 1100],
                                        ["1052", 33.05, 33.053, 4600],
                                        ["1053", 33.06, 33.054, 5900],
                                        ["1054", 33.1, 33.054, 5800],
                                        ["1055", 33.11, 33.055, 3700],
                                        ["1056", 33.11, 33.055, 500],
                                        ["1057", 33.1, 33.056, 9100],
                                        ["1058", 33.02, 33.056, 4300],
                                        ["1059", 33.08, 33.056, 1300],
                                        ["1100", 33.02, 33.055, 4500],
                                        ["1101", 33.02, 33.055, 8300],
                                        ["1102", 33.01, 33.055, 200],
                                        ["1103", 33.06, 33.055, 2900],
                                        ["1104", 33.18, 33.056, 11500],
                                        ["1105", 33.22, 33.058, 5300],
                                        ["1106", 33.27, 33.06, 5600],
                                        ["1107", 33.2, 33.061, 4700],
                                        ["1108", 33.23, 33.063, 11500],
                                        ["1109", 33.21, 33.064, 5900],
                                        ["1110", 33.21, 33.066, 1700],
                                        ["1111", 33.09, 33.066, 700],
                                        ["1112", 33.19, 33.067, 1700],
                                        ["1113", 33.1, 33.068, 3100],
                                        ["1114", 33.06, 33.068, 2700],
                                        ["1115", 33.19, 33.069, 5400],
                                        ["1116", 33.19, 33.07, 4200],
                                        ["1117", 33.19, 33.071, 0],
                                        ["1118", 33.18, 33.072, 3400],
                                        ["1119", 33.13, 33.072, 1200],
                                        ["1120", 33.12, 33.073, 3000],
                                        ["1121", 33.18, 33.074, 700],
                                        ["1122", 33.2, 33.075, 3400],
                                        ["1123", 33.22, 33.076, 2700],
                                        ["1124", 33.24, 33.078, 6400],
                                        ["1125", 33.22, 33.079, 1800],
                                        ["1126", 33.22, 33.08, 1800],
                                        ["1127", 33.22, 33.081, 3200],
                                        ["1128", 33.22, 33.082, 3100],
                                        ["1129", 33.21, 33.083, 2000],
                                        ["1130", 33.21, 33.085, 100],
                                        ["1300", 33.25, 33.086, 12900],
                                        ["1301", 33.25, 33.087, 7600],
                                        ["1302", 33.27, 33.089, 5300],
                                        ["1303", 33.25, 33.09, 7600],
                                        ["1304", 33.28, 33.092, 4000],
                                        ["1305", 33.28, 33.093, 800],
                                        ["1306", 33.26, 33.094, 3600],
                                        ["1307", 33.24, 33.095, 900],
                                        ["1308", 33.23, 33.096, 7700],
                                        ["1309", 33.22, 33.097, 3100],
                                        ["1310", 33.24, 33.098, 2100],
                                        ["1311", 33.23, 33.099, 6500],
                                        ["1312", 33.21, 33.1, 5700],
                                        ["1313", 33.2, 33.101, 38300],
                                        ["1314", 33.15, 33.101, 8000],
                                        ["1315", 33.17, 33.102, 7800],
                                        ["1316", 33.16, 33.102, 1400],
                                        ["1317", 33.15, 33.103, 7300],
                                        ["1318", 33.13, 33.103, 11500],
                                        ["1319", 33.09, 33.103, 8000],
                                        ["1320", 33.08, 33.103, 6000],
                                        ["1321", 33.06, 33.102, 6600],
                                        ["1322", 33.07, 33.102, 4700],
                                        ["1323", 33.07, 33.102, 1700],
                                        ["1324", 33.09, 33.102, 2500],
                                        ["1325", 33.08, 33.102, 3000],
                                        ["1326", 33.1, 33.102, 8500],
                                        ["1327", 33.13, 33.102, 4498],
                                        ["1328", 33.13, 33.102, 2502],
                                        ["1329", 33.07, 33.102, 4300],
                                        ["1330", 33.1, 33.102, 4500],
                                        ["1331", 33.08, 33.102, 5900],
                                        ["1332", 33.07, 33.101, 7900],
                                        ["1333", 33.1, 33.101, 6000],
                                        ["1334", 33.1, 33.101, 800],
                                        ["1335", 33.1, 33.101, 7000],
                                        ["1336", 33.1, 33.101, 2200],
                                        ["1337", 33.09, 33.101, 4900],
                                        ["1338", 33.15, 33.102, 8198],
                                        ["1339", 33.18, 33.102, 4300],
                                        ["1340", 33.17, 33.103, 3800],
                                        ["1341", 33.17, 33.103, 5800],
                                        ["1342", 33.17, 33.103, 3000],
                                        ["1343", 33.15, 33.104, 900],
                                        ["1344", 33.15, 33.104, 5100],
                                        ["1345", 33.19, 33.104, 3100],
                                        ["1346", 33.2, 33.105, 6300],
                                        ["1347", 33.23, 33.106, 9000],
                                        ["1348", 33.23, 33.106, 9600],
                                        ["1349", 33.27, 33.107, 8200],
                                        ["1350", 33.3, 33.109, 15500],
                                        ["1351", 33.27, 33.109, 7700],
                                        ["1352", 33.25, 33.11, 2800],
                                        ["1353", 33.24, 33.111, 4400],
                                        ["1354", 33.27, 33.112, 6500],
                                        ["1355", 33.24, 33.113, 2700],
                                        ["1356", 33.26, 33.113, 3500],
                                        ["1357", 33.21, 33.114, 9900],
                                        ["1358", 33.24, 33.115, 3000],
                                        ["1359", 33.24, 33.115, 2100],
                                        ["1400", 33.21, 33.116, 6600],
                                        ["1401", 33.12, 33.116, 5400],
                                        ["1402", 33.15, 33.116, 1800],
                                        ["1403", 33.15, 33.116, 3400],
                                        ["1404", 33.11, 33.116, 4800],
                                        ["1405", 33.08, 33.116, 15900],
                                        ["1406", 33.08, 33.116, 8800],
                                        ["1407", 33.09, 33.116, 4700],
                                        ["1408", 33.09, 33.116, 6100],
                                        ["1409", 33.08, 33.115, 3800],
                                        ["1410", 33.1, 33.115, 3200],
                                        ["1411", 33.12, 33.115, 3100],
                                        ["1412", 33.09, 33.115, 7100],
                                        ["1413", 33.09, 33.115, 2000],
                                        ["1414", 33.1, 33.115, 3000],
                                        ["1415", 33.11, 33.115, 3100],
                                        ["1416", 33.11, 33.115, 6700],
                                        ["1417", 33.15, 33.115, 8600],
                                        ["1418", 33.15, 33.115, 2600],
                                        ["1419", 33.15, 33.116, 6750],
                                        ["1420", 33.14, 33.116, 8650],
                                        ["1421", 33.05, 33.115, 17500],
                                        ["1422", 32.96, 33.115, 14900],
                                        ["1423", 32.92, 33.114, 15300],
                                        ["1424", 32.92, 33.113, 5700],
                                        ["1425", 32.92, 33.112, 6600],
                                        ["1426", 32.94, 33.111, 2600],
                                        ["1427", 33.0, 33.11, 7700],
                                        ["1428", 32.94, 33.11, 10400],
                                        ["1429", 32.94, 33.109, 3700],
                                        ["1430", 32.88, 33.108, 22900],
                                        ["1431", 32.89, 33.107, 28900],
                                        ["1432", 32.89, 33.106, 3500],
                                        ["1433", 32.9, 33.105, 6100],
                                        ["1434", 32.94, 33.104, 3600],
                                        ["1435", 33.05, 33.104, 8058],
                                        ["1436", 33.03, 33.103, 6300],
                                        ["1437", 33.02, 33.103, 1300],
                                        ["1438", 33.02, 33.103, 4400],
                                        ["1439", 33.08, 33.102, 10800],
                                        ["1440", 33.05, 33.102, 2400],
                                        ["1441", 33.03, 33.102, 5400],
                                        ["1442", 33.0, 33.101, 9200],
                                        ["1443", 33.0, 33.101, 3442],
                                        ["1444", 32.96, 33.1, 3500],
                                        ["1445", 32.99, 33.1, 4100],
                                        ["1446", 32.95, 33.099, 2900],
                                        ["1447", 32.94, 33.099, 5000],
                                        ["1448", 32.94, 33.098, 10700],
                                        ["1449", 32.95, 33.097, 20800],
                                        ["1450", 32.92, 33.096, 10100],
                                        ["1451", 32.9, 33.096, 6400],
                                        ["1452", 32.95, 33.095, 10100],
                                        ["1453", 32.9, 33.094, 18300],
                                        ["1454", 32.88, 33.093, 5800],
                                        ["1455", 32.9, 33.092, 4500],
                                        ["1456", 32.89, 33.092, 15100],
                                        ["1457", 32.88, 33.091, 1400],
                                        ["1458", 32.88, 33.09, 0],
                                        ["1459", 32.88, 33.089, 0],
                                        ["1500", 32.88, 33.088, 15025]
                                        ],
                "yestclose": 33.01}
        return JsonResponse(info)
    else:
        return JsonResponse({"statue": 500, "data": []})


@check_login
def search(request, code):
    response = sys_requests.get(url='http://hq.sinajs.cn/list=' + code)
    line = response.text.split("\"")[1]
    return HttpResponse(str(line))
